package com.car.utils;

public class CurrentHolder {

    private static final ThreadLocal<Integer> ID_LOCAL = new ThreadLocal<>(); // 存储用户ID
    private static final ThreadLocal<Integer> USER_TYPE_LOCAL = new ThreadLocal<>(); // 存储用户类型: 1-用户, 2-员工, 3-管理员

    public static void setCurrentId(Integer id) {
        ID_LOCAL.set(id);
    }

    public static Integer getCurrentId() {
        return ID_LOCAL.get();
    }

    public static void setUserType(Integer userType) {
        USER_TYPE_LOCAL.set(userType);
    }

    public static Integer getUserType() {
        return USER_TYPE_LOCAL.get();
    }

    // 判断是否为管理员
    public static boolean isAdmin() {
        Integer type = USER_TYPE_LOCAL.get();
        return type != null && type == 3;
    }

    // 判断是否为普通用户
    public static boolean isUser() {
        Integer type = USER_TYPE_LOCAL.get();
        return type != null && type == 1;
    }

    // 判断是否为员工
    public static boolean isEmployee() {
        Integer type = USER_TYPE_LOCAL.get();
        return type != null && type == 2;
    }

    public static void remove() {
        ID_LOCAL.remove();
        USER_TYPE_LOCAL.remove();
    }
}
